package view;

import java.awt.EventQueue;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.Font;

import utils.ScreenAdjustment;


//@author A0103947A
public class SavingsAnalysis {

	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SavingsAnalysis window = new SavingsAnalysis(500, 3, 3, 5, 10, 20);
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	// Function that changes a double value to String with exactly two decimals
	/**
	 * 
	 * @param valueToFormat
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	private String returnTwoDecimals(double valueToFormat) {
		NumberFormat df = DecimalFormat.getInstance();
        df.setMinimumFractionDigits(2);
        df.setMaximumFractionDigits(2);
        return (df.format(valueToFormat));
	}
	
	// Function that calculates the total amount of an investment at a given interest rate
	// Over time
	/**
	 * 
	 * @param principal
	 * @param interestRate
	 * @param years
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	private double calculateInvestment(double principal, double interestRate, double years) {
		double finalAmount = principal * Math.pow((1+interestRate/100), years);
		
		//double interest = principal*interestRate/100 *years;
		//double finalAmount = principal + interest;
		return finalAmount;
	}
	
	// Attributes
	private double _amountToInvest;
	private double _interestRate;
	private int _totalTimePeriods;
	private double _firstPeriod;
	private double _secondPeriod;
	private double _thirdPeriod;
	private double _amountFirstPeriod;
	private double _amountSecondPeriod;
	private double _amountThirdPeriod;
	
	private boolean _investmentAdded;
	//private Savings _currSavings;


	/**
	 * Create the application.
	 */
	/**
	 * @param startingAmount
	 * @param rate
	 * @param numberOfTimePeriods
	 * @param calculateFirst
	 * @param calculateSecond
	 * @param calculateThird
	 */
	public SavingsAnalysis(double startingAmount, double rate, int numberOfTimePeriods, double calculateFirst, double calculateSecond, double calculateThird) {
		_amountToInvest = startingAmount;
		_interestRate = rate;
		
		_investmentAdded = false;
		//_currSavings = savings;
		
		_totalTimePeriods = numberOfTimePeriods;
		
		// If only one time period are entered, set variables related to 
		// Second and third time periods to 0
		if (_totalTimePeriods == 1) {
			_firstPeriod = calculateFirst;
			_secondPeriod = 0;
			_thirdPeriod = 0;
			
			_amountFirstPeriod = calculateInvestment(_amountToInvest, _interestRate, _firstPeriod);
			_amountSecondPeriod = 0;
			_amountThirdPeriod = 0;
		}
		// If only two time period are entered, set variables related to 
		// Second and third time periods to 0
		else if (_totalTimePeriods == 2) {
			_firstPeriod = calculateFirst;
			_secondPeriod = calculateSecond;
			_thirdPeriod = 0;
			
			_amountFirstPeriod = calculateInvestment(_amountToInvest, _interestRate, _firstPeriod);
			_amountSecondPeriod = calculateInvestment(_amountToInvest, _interestRate, _secondPeriod);
			_amountThirdPeriod = 0;
		}
		// If all three time periods are entered, calculate all variables
		// Related to all three time periods
		else {
			_firstPeriod = calculateFirst;
			_secondPeriod = calculateSecond;
			_thirdPeriod = calculateThird;
			
			_amountFirstPeriod = calculateInvestment(_amountToInvest, _interestRate, _firstPeriod);
			_amountSecondPeriod = calculateInvestment(_amountToInvest, _interestRate, _secondPeriod);
			_amountThirdPeriod = calculateInvestment(_amountToInvest, _interestRate, _thirdPeriod);
		}
		
		initialize();
	}
	
	/**
	 * 
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public boolean checkInvestmentAdded() {
		return _investmentAdded;
	}
	
//	public Savings getSavings() {
//		return _currSavings;
//	}

	/**
	 * Initialize the contents of the frame.
	 */
	/**
	 * 
	 * @Precondition 
	 * @Postcondition 
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(ScreenAdjustment.getAlignCenterX(450), ScreenAdjustment.getAlignCenterY(300), 450, 300);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel lblTitle = new JLabel("Savings Analysis");
		lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblTitle.setBounds(115, 23, 166, 25);
		lblTitle.setFont(new Font("Century Gothic", Font.PLAIN, 20));
		frame.getContentPane().add(lblTitle);
		
		JLabel lblAmountInvested = new JLabel("Amount To Invest: ");
		lblAmountInvested.setHorizontalAlignment(SwingConstants.TRAILING);
		lblAmountInvested.setBounds(115, 69, 120, 16);
		frame.getContentPane().add(lblAmountInvested);
		
		JLabel labelValueInvested = new JLabel("$" + returnTwoDecimals(_amountToInvest));
		labelValueInvested.setBounds(240, 69, 81, 16);
		frame.getContentPane().add(labelValueInvested);
		
		JLabel lblInterestRate = new JLabel("Annual Interest Rate To Invest At: ");
		lblInterestRate.setHorizontalAlignment(SwingConstants.TRAILING);
		lblInterestRate.setBounds(20, 90, 215, 16);
		frame.getContentPane().add(lblInterestRate);
		
		JLabel lblValue_interestRate = new JLabel(returnTwoDecimals(_interestRate) + "%");
		lblValue_interestRate.setBounds(240, 90, 81, 16);
		frame.getContentPane().add(lblValue_interestRate);
		
		JLabel lblInitialInvestment = new JLabel("Initial amount: " + returnTwoDecimals(_amountToInvest));
		lblInitialInvestment.setBounds(164, 130, 252, 16);
		frame.getContentPane().add(lblInitialInvestment);
		
		JLabel lblAfterFirstPeriod = new JLabel("Amount after " + _firstPeriod + " years: $" + returnTwoDecimals(_amountFirstPeriod));
		lblAfterFirstPeriod.setBounds(116, 157, 300, 16);
		frame.getContentPane().add(lblAfterFirstPeriod);
		
		JLabel lblAfterSecondPeriod = new JLabel("Amount after " + _secondPeriod + " years: $" + returnTwoDecimals(_amountSecondPeriod));
		// If only one time period is entered, set this field to not visible
		if (_totalTimePeriods == 1) {
			lblAfterSecondPeriod.setVisible(false);
		}
		lblAfterSecondPeriod.setBounds(116, 185, 300, 16);
		frame.getContentPane().add(lblAfterSecondPeriod);
		
		JLabel lblAfterThirdPeriod = new JLabel("Amount after " + _thirdPeriod + " years: $" + returnTwoDecimals(_amountThirdPeriod));
		// If only one or two time periods are entered, set this field to not visible
			if (_totalTimePeriods == 1 || _totalTimePeriods == 2) {
				lblAfterThirdPeriod.setVisible(false);
			}
		lblAfterThirdPeriod.setBounds(116, 213, 300, 16);
		frame.getContentPane().add(lblAfterThirdPeriod);
	}
}
